$.fn.extend({
    bodyBsLoading: function(e) {
        var t = '<div class="box-bs-loading"><div class="spinner-border text-primary spinner-border-center" role="status">\n                    <span class="sr-only">Loading...</span>\n                </div><h5>' + e + "</h5></div>";
        $(this).append(t)
    },
    bodyBsLoadingReset: function() {
        $(this).find(".box-bs-loading").remove()
    },
    btnReset: function(e) {
        $(this).attr("disabled", !1), e && $(this).html(e), $(this).find(".spinner-border").remove()
    },
    btnLoading: function(e, t, a, o) {
        var t = t || "spinner-border-sm",
            n = o;
        $(this).attr("disabled", !0);
        var i = '<span class="spinner-border ' + t + '" role="status" aria-hidden="true"></span>';
        e ? $(this).html(i + e) : a ? $(this).html(i) : n ? $(this).html(i + $(this).html()) : $(this).prepend(i)
    },
});

// 菜单切换
$("#navSidebarTabContent li").hover(function() {
    $(this).find(".nav-popover").show()
}, function() {
    $(this).find(".nav-popover").hide()
});

$(".login-modal").on('click',function (){

    $.ajax({
        type: "POST",
        url: "/user/get_login_qrcode",
        dataType: "json",
        success: function(res) {
            if (res.code == 200) {
                $("#modalMobileLogin").hide();
                $("#modalQrcodeLogin").show();
                $("#modalMobileNumberLogin").show();

                $("#loginWechat img").attr("src",res.data.qrcode);

                var time = 1000;
                setInterval(function (){
                    $.post("/user/check_weixin_login",{"token" : res.data.token},function (res2){
                        if (res2.code == 200)
                        {
                             location.href = location.href;
                        }
                    });
                },time);
            }
        }
    });

});

$(".reg-modal").on("click",function() {
    $("#modalMobileLogin").show();
    $("#modalQrcodeLogin").hide();
    $("#modalMobileNumberLogin").show();
});

$(".modalMobileLogin").on("click",function() {
    $("#modalMobileLogin").show();
    $("#modalQrcodeLogin").hide();
    $("#modalMobileNumberLogin").show();
});

$("#logOut").on("click",function() {
    $("body").bodyBsLoading("退出中，请稍候。。。");

    $.ajax({
        type: "POST",
        url: "/user/logout",
        dataType: "json",
        success: function(t) {
            $("body").bodyBsLoadingReset();
            location.href = location.href;
        },
        error: function() {
             $("body").bodyBsLoadingReset();
        }
    })
});

$("#loginBtn").on("click",function() {

    var loginPhone = $("#loginPhone").val();
    var loginVericode = $("#loginVericode").val();

    $.ajax({
        type: "POST",
        url: "/user/login",
        data: {
            'phone' : loginPhone,
            'code' : loginVericode
        },
        dataType: "json",
        success: function(res) {
            if (res.code == 200) {
                location.href = location.href;
            }else {
                layer.msg(res.msg);
            }
        }
    })
});

// 下载按钮
$(".download-action").on("click",function() {
    var id = $(this).data('id');
    $.ajax({
        type: "POST",
        url: "/download",
        data: {
            'id' : id
        },
        dataType: "json",
        success: function(res) {
            if (res.code == 200) {
                if (res.data != "")
                {
                    window.open(res.data);
                }else {
                    layer.msg("下载失败请联系管理员!");
                }
               //  location.href = res.data;
            }
        }
    })
});



function getFixedWindow() {
    var e = document.createElement("div");
    e.className = "toast fixed-nav", e.setAttribute("role", "alert"), e.setAttribute("aria-live", "assertive"), e.setAttribute("aria-atomic", "true");
    var t = '    <div class="toast-header">\n        <strong class="mr-auto"></strong>\n        <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">\n            <span aria-hidden="true">&times;</span>\n        </button>\n    </div>\n    <div class="toast-body">\n        <ul>\n            <li>\n                <a class="text-decoration-none" href="/act/2020/authenticate.html">\n                    <i class="iconfont">&#xe616;</i>\n                    上传赚钱\n                </a>\n            </li>\n            <li>\n                <a class="text-decoration-none" href="/help/index.html">\n                    <i class="iconfont">&#xe674;</i>\n                    帮助中心\n                </a>\n            </li>\n            <li>\n                <a class="text-decoration-none" href="/help/index.html#联系我们">\n                    <i class="iconfont">&#xe673;</i>\n                    官方客服\n                </a>\n            </li>\n            <li>\n                <a class="text-decoration-none" href="/help/index.html#意见反馈">\n                    <i class="iconfont">&#xe675;</i>\n                    意见反馈\n                </a>\n            </li>\n            <li>\n                <a class="text-decoration-none" id="goToTop" style="display: none">\n                    <i class="iconfont">&#xe671;</i>\n                    返回顶部\n                </a>\n            </li>\n        </ul>\n    </div>\n';
    e.innerHTML = t, $("body").append(e), $(e).toast({
        autohide: !1
    }), $(e).toast("show")
}

function scrollLoadingAnimation() {
    var e = $(window).scrollTop(),
        t = $(window).outerHeight(!0),
        a = 200;
    $(".animation-t,.animation-b,.animation-l,.animation-r").each(function(o, n) {
        var i = $(n).offset().top,
            s = parseInt($(n).attr("data-delay"));
        i > e && e + t > i && (s && s > 0 ? setTimeout(function() {
            $(n).removeClass("animation-t animation-b animation-l animation-r")
        }, s * a) : $(n).removeClass("animation-t animation-b animation-l animation-r"))
    }), $(window).scroll(function() {
        $(".animation-t,.animation-b,.animation-l,.animation-r").each(function(e, o) {
            var n = $(o).offset().top,
                i = parseInt($(o).attr("data-delay"));
            n > $(window).scrollTop() && n < $(window).scrollTop() + t && (i && i > 0 ? setTimeout(function() {
                $(o).removeClass("animation-t animation-b animation-l animation-r")
            }, i * a) : $(o).removeClass("animation-t animation-b animation-l animation-r"))
        })
    })
}

function createQrcode(text,node) {
    $("#" + node).html("");
    new QRCode(node, {
        text: text,
        width: 250,
        height: 250,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H,
        render: "canvas"
    })
}


function testSpicel(e) {
    var t = /[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、]/im;
    return t.test(e) ? !0 : !1
}

function search(e, t, a) {
    $(t).attr("data-target");
    var o = $(t).html();
    if (e) {
        if (e.length > 50) return messageAlert.bsError("搜索词不能大于50个字符"), void 0;
        if (testSpicel(e)) return messageAlert.bsError("搜索词中不能包含特殊字符"), void 0;
        if ($(t).btnLoading("", "", !0), a && "result" === a) {
            var n = window.location.href; - 1 === n.indexOf("?") ? (window.location.href = n + "?word=" + e, $(t).btnReset(o)) : (window.location.href = n + "&word=" + e, $(t).btnReset(o))
        } else window.location.href = "/search?word=" + e, $(t).btnReset(o)
    }
}

function searchBtn() {
    $('[data-btn="search"]').each(function(e, t) {
        var a = $(t).attr("data-target"),
            o = $(t).attr("data-type") || "";
        $("#" + a).bind("keyup", function(e) {
            var a = $.trim($(this).val());
            ("13" === e.keyCode || 13 === e.keyCode) && search(a, t, o)
        })
    }), $('[data-btn="search"]').click(function() {
        var e = $(this).attr("data-target"),
            t = $(this).attr("data-type") || "",
            a = $.trim($("#" + e).val());
        search(a, this, t)
    })
}

searchBtn();